package com.zjw.zy.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.zjw.zy.setting.SettingStorage
import com.zjw.zy.base.BaseViewModel
import com.zjw.zy.bean.ImageInfo
import com.zjw.zy.ext.launchInIo
import com.zjw.zy.setting.StartUpImagePathOption
import com.zjw.zy.utils.PathUtils

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/10/27 10:42
 * @description：
 */
class ImageContentViewModel : BaseViewModel() {

    private val mImagePaths = MutableLiveData<List<ImageInfo>>()
    fun getImageInfoList(): LiveData<List<ImageInfo>> = mImagePaths

    private var mImageRootPath = ""

    override fun onPageEnter() {
        super.onPageEnter()
    }

    override fun onPageExit() {
        super.onPageExit()
    }

    fun deleteImageInfo(collection: Collection<ImageInfo>) {
        launchInIo {
            PathUtils.deletePath(collection.map { it.path })

            mImagePaths.postValue(PathUtils.getImageInfoListFromPath(mImageRootPath))
        }
    }

    fun initRootPath(path: String) {
        mImageRootPath = path
        launchInIo {
            PathUtils.getImageInfoListFromPath(path).let { data ->
                mImagePaths.postValue(data)
            }
        }
    }

    //设置启动页
    fun setStartUpImage(path: ImageInfo) {
        launchInIo {
            SettingStorage.setOption(
                StartUpImagePathOption()
                    .apply { this.value = path.path })
        }
    }
}